package com.ztgf.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.order.models.service.common.ICommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.ztgf.order.models.service.IOrderStatusService;
import com.ztgf.order.models.bo.OrderStatusBO;
import com.ztgf.order.models.dto.OrderStatusDTO;

/**
 * 订单状态表控制器
 *
 * @author YUE
 * @Date 2020-06-23 10:33:48
 */

@RestController
@RequestMapping("/api/orderStatus")
@Slf4j
@Api(value = "OrderStatusApiController", tags = "订单状态表Api接口")
public class OrderStatusApiController {

    @DubboReference
    private IOrderStatusService orderStatusService;
    @DubboReference
    private ICommonService commonService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<OrderStatusDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        OrderStatusDTO dto = orderStatusService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "订单状态表列表", notes = "订单状态表列表")
    @PostMapping("/queryPageList")
    public Result<Page<OrderStatusDTO>> queryPageList(OrderStatusBO bo) {
        Wrapper<OrderStatusDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<OrderStatusDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = orderStatusService.selectPage(page, wrapper);

        return new Result<>(page);
    }


    @PostMapping("/updateReceived")
    @ApiOperation(value = "订单签收", notes = "订单签收")
    public Result updateReceived(Long orderId) {
        if (orderId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
//        return commonService.saveReceive(orderId, true);
        return orderStatusService.saveOrderReceive(orderId, true, true);

    }
}
